/**
 * Filename: HomeAlbumFragment.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/2/29 17:55
 */
package com.github.postsanf.yinian.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.AlbumStatusActivity;
import com.github.postsanf.yinian.activity.CreateZoneActivity;
import com.github.postsanf.yinian.activity.pop.PopMenu;
import com.github.postsanf.yinian.activity.pop.PopMenuItem;
import com.github.postsanf.yinian.adapter.AlbumListAdapter;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.service.AlbumService;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.github.postsanf.yinian.utils.SpUtils;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.UIHelper;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.nineoldandroids.animation.ObjectAnimator;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class HomeAlbumFragment extends BaseFragment {
    private View view;                       // 上下文视图
    private RecyclerView rv_home_album;              // 列表视图
    private RelativeLayout empty_home_album;              // 列表视图
    private AlbumListAdapter adapter;                    // 适配器
    private boolean isSwitch = false;           // 是否切换排列模式  false 默认  true 双排
    private SharedPreferences sp_data;
    private List<YNAlbum> albumList = new ArrayList<>();
    private RelativeLayout rl_toolbar;
    private PopMenu mPopMenu;
    private TextView titlt_top;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerBroadcastReceiver();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        sp_data = application.getSp_data();
        isSwitch = sp_data.getBoolean(getString(R.string.ALBUM_SWITCH), false);
        view = inflater.inflate(R.layout.yn_fragment_home_container, null);
        initPop();
        rl_toolbar = ((RelativeLayout) view.findViewById(R.id.rl_toolbar));
        titlt_top = ((TextView) view.findViewById(R.id.titlt_top));
        titlt_top.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fzqk.ttf"));
        titlt_top.setText("忆年");
        view.findViewById(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop();
            }
        });
        rv_home_album = (RecyclerView) view.findViewById(R.id.rv_home_list_album);
        empty_home_album = (RelativeLayout) view.findViewById(R.id.empty_home_album);
        setSwitchAdapter();
        adapter = new AlbumListAdapter(HomeAlbumFragment.this, getActivity(), albumList, groupsnew);
        rv_home_album.setAdapter(adapter);
        noticeServiceInit();
        return view;
    }

    public void showPop() {
        Intent intent = new Intent(ACTIONs.record);
        intent.putExtra("key", "createAlbum_4");
        intent.putExtra("value", 1);
        getActivity().sendBroadcast(intent);
        mPopMenu.show(rl_toolbar);
    }

    private void initPop() {
        mPopMenu = new PopMenu(getActivity(), UIHelper.dip2px(getActivity(), 125),
                UIHelper.dip2px(getActivity(), 84));
        mPopMenu.setItemOnClickListener(onitemClick);
        mPopMenu.addAction(new PopMenuItem(getActivity(), R.string.menu_item_new,
                R.drawable.menu_item_new));
//        mPopMenu.addAction(new PopMenuItem(getActivity(), R.string.menu_item_join,
//                R.drawable.menu_item_join));
        mPopMenu.addAction(new PopMenuItem(getActivity(), R.string.menu_item_sao,
                R.drawable.menu_item_erweima));
//        mPopMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//            }
//        });
    }

    private PopMenu.OnItemOnClickListener onitemClick = new PopMenu.OnItemOnClickListener() {
        @Override
        public void onItemClick(PopMenuItem item, int position) {
            switch (position) {
                case 0:// 新建相册
                    intent2Activity(CreateZoneActivity.class);
                    break;
//                case 1:// 申请加入
////                    intent2Activity(JoinAlbumActivity.class);
//                    intent2Activity(ApplyJoinActivity.class);
//                    break;
                case 1:
                    getActivity().sendBroadcast(new Intent("mainerweima"));
                    break;
                default:
                    break;
            }
        }
    };

    private String groupsnew;

    private void GetNewEvent() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynGetNewEvent))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject sjson = new JSONObject(s);
                            if (ReqUtils.isSuccess(sjson.optString("code"))) {
                                groupsnew = sjson.optJSONArray("data").optJSONObject(0).optString("groupid");
                                Log.i("xjs", "home+groupsnew==>" + groupsnew);
                                if (!StringUtils.isEmpty(groupsnew)) {
                                    if (groupsnew.contains(",")) {
                                        String[] ggsnew = groupsnew.split(",");
                                        for (int i = 0; i < ggsnew.length; i++) {
                                            SpUtils.put(getActivity(), ggsnew[i], true);
                                        }
                                    } else {
                                        SpUtils.put(getActivity(), groupsnew, true);
                                    }
                                }
                            }
                            albumList.clear();
                            albumList.addAll(YNSingleton.getInstance().getAlbumList());
                            if (albumList.size() > 0) {
                                YNAlbum ynAlbumnull = new YNAlbum();
                                ynAlbumnull.setGpic("空图创建");
                                albumList.add(ynAlbumnull);
                                adapter.notifyDataSetChanged();
                                InitHuoDongAlbum();
                                switchPageNull(false);
//                            adapter = new AlbumListAdapter(HomeAlbumFragment.this, getActivity(), albumList, groupsnew);
//                            rv_home_album.setAdapter(adapter);
//                            adapter.notifyDataSetChanged();
                            } else {
                                switchPageNull(true);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        Toast.makeText(getActivity(), CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        liteHttp.executeAsync(req);
    }


    private void switchPageNull(final boolean isNull) {
        if (isNull) {
            rv_home_album.setVisibility(View.GONE);
            empty_home_album.setVisibility(View.VISIBLE);
        } else {
            rv_home_album.setVisibility(View.VISIBLE);
            empty_home_album.setVisibility(View.GONE);
        }
    }


    boolean isCantrag = true;
    boolean isCantrag22 = false;

    // switch==true 双排  switch==false 单排
    private void setSwitchAdapter() {
//        if (isSwitch) {
//            rv_home_album.setLayoutManager(new WrapContentGridLayoutManager(getActivity(), 2));
//            adapter = new AlbumListAdapter(this, getActivity(), albumList, true, groupsnew);
//            rv_home_album.setAdapter(adapter);
//            ItemDragHelperCallback callbacktwo = new ItemDragHelperCallback() {
//                private RecyclerView.ViewHolder vh;
//
//                @Override
//                public boolean isLongPressDragEnabled() {
//                    return true;
//                }
//
//                @Override
//                public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder
//                        viewHolder) {
//                    int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT |
//                            ItemTouchHelper.RIGHT;
//                    return makeMovementFlags(dragFlags, 0);
//                }
//
//                @Override
//                public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
//                    super.onSelectedChanged(viewHolder, actionState);
//                    if (viewHolder != null) {
//                        vh = viewHolder;
//                        pickUpAnimation(viewHolder.itemView);
//                    } else {
//                        if (vh != null) {
//                            putDownAnimation(vh.itemView);
//                        }
//                    }
//                }
//
//                @Override
//                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
//                                      RecyclerView.ViewHolder target) {
//                    Collections.swap(albumList, viewHolder.getAdapterPosition(), target
//                            .getAdapterPosition());
//                    return true;
//                }
//
//                @Override
//                public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int
//                        fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
//                    super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
//                    adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target
//                            .getAdapterPosition());
//                    getActivity().sendBroadcast(new Intent(ACTIONs.actionAlbumSort));
//                }
//            };
//            new ItemTouchHelper(callbacktwo).attachToRecyclerView(rv_home_album);
//        } else {
        rv_home_album.setLayoutManager(new LinearLayoutManager(getActivity()));
        ItemTouchHelper.Callback callbackone = new ItemTouchHelper.Callback() {

            private RecyclerView.ViewHolder vh;

            @Override
            public boolean isLongPressDragEnabled() {
                return isCantrag;
            }

            @Override
            public boolean isItemViewSwipeEnabled() {
                return false;
            }

            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder
                    viewHolder) {
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (viewHolder != null) {
                    vh = viewHolder;
                    pickUpAnimation(viewHolder.itemView);
                } else {
                    if (vh != null) {
                        putDownAnimation(vh.itemView);
                    }
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                                  RecyclerView.ViewHolder target) {
                if ((target.getAdapterPosition() == 0 && isCantrag22) || (target.getAdapterPosition() > 0 && target.getAdapterPosition() == albumList.size() - 1)) {
//                    Toast.makeText(getActivity(),"不可移到创建空间后面",Toast.LENGTH_SHORT).show();
                    return false;
                } else {
                    Collections.swap(albumList, viewHolder.getAdapterPosition(), target
                            .getAdapterPosition());
                }
                return true;
            }

            @Override
            public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int
                    fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
                super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
                adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target
                        .getAdapterPosition());
                getActivity().sendBroadcast(new Intent(ACTIONs.actionAlbumSort));
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                albumList.remove(viewHolder.getAdapterPosition());
                adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
            }
        };
        new ItemTouchHelper(callbackone).attachToRecyclerView(rv_home_album);
//        }
        rv_home_album.setHasFixedSize(true);
    }


    private void pickUpAnimation(View view) {
        Animation translateAnimationtab = new TranslateAnimation(0, 10, 0, 0);
        translateAnimationtab.setInterpolator(new CycleInterpolator(20));
        translateAnimationtab.setDuration(300);
        view.startAnimation(translateAnimationtab);
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationZ", 1f, 20f);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(300);
        animator.start();
    }

    private void putDownAnimation(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationZ", 20f, 1f);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.setDuration(300);
        animator.start();


    }

    // 更新相册数据
    private void updateView(YNAlbum albumItem, Boolean isAdd) {
        if (isAdd) {
//            albumList.add(albumItem);
//            adapter.notifyItemInserted(adapter.getItemCount());
            noticeServiceInit();
        } else {
            for (int i = 0; i < albumList.size(); i++) {
                if (albumList.get(i).getGroupid() == albumItem.getGroupid()) {
                    albumList.remove(i);
                    adapter.notifyItemRemoved(adapter.getHeaderView() == null ? i : i + 1);
                    break;
                }
            }
        }
        if (albumList.size() > 0) {
            switchPageNull(false);
        } else {
            switchPageNull(true);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CommonConstants.REQ_SORT:
                List<String> ss = new ArrayList<>();
                for (int i = 0; i < albumList.size(); i++) {
                    ss.add(albumList.get(i).getGroupid() + "");
                }
                HashMap<String, List<String>> order = new HashMap<>();
                order.put(CommonConstants.YNS_ORDER, ss);
                sortAlbum(gson.toJson(order));
                break;
            default:
                break;
        }
    }

    private void sortAlbum(String sortstr) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_ORDER, sortstr);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynSortAlbums))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
//                        YNCommonResponse resSort = gson.fromJson(s, YNCommonResponse.class);
//                        if (resSort != null && ReqUtils.isSuccess(resSort.getCode())) {
//                            albumList.clear();
//                            albumList.addAll(albumList);
//                            adapter.notifyDataSetChanged();
//                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        Toast.makeText(getActivity(), CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 分析并执行动作
    BroadcastReceiver mCameraBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "isCantrag":
                    isCantrag = intent.getBooleanExtra("isCantrag", false);
                    break;
                case ACTIONs.actionUpdateAlbum:
                    Log.i("xjs", " ACTIONs.actionUpdateAlbum");
                    GetNewEvent();
//                    albumList.clear();
//                    albumList.addAll(YNSingleton.getInstance().getAlbumList());
//                    adapter.notifyDataSetChanged();
//                    if (albumList.size() > 0) {
//                        switchPageNull(false);
//                    } else {
//                        switchPageNull(true);
//                    }
                    break;
                case ACTIONs.actionAlbumItem:
                    Log.i("xjs", " ACTIONs.actionAlbumItem");
                    YNAlbum albumItem = (YNAlbum) intent.getSerializableExtra(ACTIONs.aAlbumItem);
                    boolean isAdd = intent.getExtras().getBoolean(ACTIONs.IsAdd);
                    updateView(albumItem, isAdd);
                    break;
                case ACTIONs.actionAlbumSwitch:
                    // 切换状态并保存
                    isSwitch = !isSwitch;
                    SharedPreferences.Editor ed = application.getSp_data().edit();
                    ed.putBoolean(getString(R.string.ALBUM_SWITCH), isSwitch);
                    ed.commit();
                    break;
                case ACTIONs.actionInvateGroup:
                    YNAlbum addItem = (YNAlbum) intent.getSerializableExtra(ACTIONs.aAlbumItem);
                    boolean add = intent.getExtras().getBoolean(ACTIONs.IsAdd);
                    updateView(addItem, add);
                    break;
                case ACTIONs.actionIsNewUser:
//                    boolean isNew = intent.getBooleanExtra(ACTIONs.aAddData, false);
//                    switchPage(isNew);
                    break;
                // 修改相册名
                case ACTIONs.actionAlertGName:
                    updateAlbum(true);
                    break;
                // 修改相册背景
                case ACTIONs.actionAlertGPic:
                    updateAlbum(false);
                    break;
                case ACTIONs.actionTarget:
                    Intent tintent = new Intent(getActivity(), AlbumStatusActivity.class);
                    tintent.putExtra(ACTIONs.aAddData, false);
                    startActivity(tintent);
                    break;
                case ACTIONs.actionAlbumSort:
                    List<String> ss = new ArrayList<>();
                    ss.clear();
                    for (int i = 0; i < albumList.size(); i++) {
                        if (albumList.get(i).getGroupid() > 0)
                            ss.add(albumList.get(i).getGroupid() + "");
                    }
                    HashMap<String, List<String>> order = new HashMap<>();
                    order.put(CommonConstants.YNS_ORDER, ss);
                    sortAlbum(gson.toJson(order));
                    break;
                case ACTIONs.actionAlbumInit:
                    Log.i("xjs", "actionAlbumInit");
                    noticeServiceInit();
                    break;
                case ACTIONs.actionAlbumNotice:
                    Log.i("xjs", "actionAlbumNotice");
                    int groupid = intent.getIntExtra("groupid", 0);
                    for (YNAlbum ynAlbum : albumList) {
                        if (ynAlbum.getGroupid() == groupid) {
                            YNSingleton.getInstance().setCurAlbum(ynAlbum);
                            application.setCurGid(ynAlbum.getGroupid());
//                            context.startActivity(new Intent(context, PeopleSpaceActivity2.class).putExtra(ACTIONs.aAddData, true));
                            getActivity().sendBroadcast(new Intent(ACTIONs.actionUpdateFristAlbum).putExtra("update", true));
                            getActivity().sendBroadcast(new Intent(ACTIONs.actionToPeopleTimeLine));
                            ynAlbum.setGmnotify(0);
                            SpUtils.put(context, String.valueOf(ynAlbum.getGroupid()), false);
                        }
                    }
                    noticeServiceInit();
                    break;
                case ACTIONs.actionGroupQuit:
                    noticeServiceInit();
                    break;
                case ACTIONs.actionUpdateNEwsHUanXin:
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    // 更新相册信息
    private void updateAlbum(boolean isName) {
        for (int i = 0; i < albumList.size(); i++) {
            if (albumList.get(i).equals(YNSingleton.getInstance().getCurAlbum())) {
                if (isName) {
                    albumList.get(i).setGname(YNSingleton.getInstance().getCurAlbum().getGname());
                } else {
                    albumList.get(i).setGpic(YNSingleton.getInstance().getCurAlbum().getGpic());
                }
                adapter.notifyItemChanged(adapter.getHeaderView() == null ? i : i + 1);
            }
        }
    }

    // 注册接收者
    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionUpdateAlbum);
        myIntentFilter.addAction(ACTIONs.actionAlbumItem);
        myIntentFilter.addAction(ACTIONs.actionInvateGroup);
        myIntentFilter.addAction(ACTIONs.actionAlbumSwitch);
        myIntentFilter.addAction(ACTIONs.actionAlertGName);
        myIntentFilter.addAction(ACTIONs.actionAlertGPic);
        myIntentFilter.addAction(ACTIONs.actionTarget);
        myIntentFilter.addAction(ACTIONs.actionStatus);
        myIntentFilter.addAction(ACTIONs.actionIsNewUser);
        myIntentFilter.addAction(ACTIONs.actionAlbumSort);
        myIntentFilter.addAction(ACTIONs.actionGroupQuit);
        myIntentFilter.addAction(ACTIONs.actionAlbumInit);
        myIntentFilter.addAction(ACTIONs.actionAlbumNotice);
        myIntentFilter.addAction(ACTIONs.actionUpdateNEwsHUanXin);
        myIntentFilter.addAction("isCantrag");
        getActivity().registerReceiver(mCameraBroadcastReceiver, myIntentFilter);
    }

    // 通知后台加载数据
    private void noticeServiceInit() {
        Intent mIntent = new Intent(getActivity(), AlbumService.class);
        mIntent.setAction(ACTIONs.actionGetAlbum);
        mIntent.putExtra(ACTIONs.aUid, application.getCurID());
        getActivity().startService(mIntent);
    }

    private void InitHuoDongAlbum() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/activity/ShowActivitySpace")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "huodong===>" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.optInt("code") == 0) {
                                if (jsonObject.optJSONArray("data") != null && jsonObject.optJSONArray("data").length() > 0) {
                                    isCantrag22 = true;
                                    YNAlbumResponse ynAlbumResponse = gson.fromJson(s, YNAlbumResponse.class);
                                    final YNAlbum resGroup = ynAlbumResponse.getData().get(0);
                                    resGroup.setHuoDong(true);
                                    for (int i = 0; i < albumList.size(); i++) {
                                        if (albumList.get(i).isHuoDong()) {
                                            albumList.remove(i);
                                        }
                                    }
                                    albumList.add(0, resGroup);
                                    adapter.notifyItemRangeInserted(0, 1);
                                    adapter.notifyDataSetChanged();
//                                    RecyclerViewHeader headview = RecyclerViewHeader.fromXml(getActivity(), R.layout.yn_item_album);
//                                    View headview = LayoutInflater.from(getActivity()).inflate(R.layout.yn_item_album_huodong, null);
//                                    headview.findViewById(R.id.ll_item_album).setOnClickListener(new View.OnClickListener() {
//                                        @Override
//                                        public void onClick(View v) {
//                                            getActivity().startActivity(new Intent(getActivity(), TimeLineStatusFristActivity.class).putExtra("StatusFrist", resGroup));
//                                        }
//                                    });
//                                    headview.findViewById(R.id.ll_item_album).setOnLongClickListener(new View.OnLongClickListener() {
//                                        @Override
//                                        public boolean onLongClick(View v) {
//                                            getActivity().sendBroadcast(new Intent("isCantrag").putExtra("isCantrag", false));
//                                            return true;
//                                        }
//                                    });
//                                    SimpleDraweeView iv_item_album_bg = (SimpleDraweeView) headview
//                                            .findViewById(R.id.iv_item_album_bg);
//                                    ImageOptHelper.showFrescoHome(Uri.parse(resGroup.getGpic()), iv_item_album_bg, getActivity());
//                                    ImageView iv_show_new_status_tip = (ImageView) headview
//                                            .findViewById(R.id.iv_show_new_status_tip);
//                                    iv_show_new_status_tip.setImageResource(R.drawable.icon_huodongtip);
//                                    iv_show_new_status_tip.setVisibility(View.VISIBLE);
//                                    TextView item_memorytitle = (TextView) headview.findViewById(R.id.item_memorytitle);
//                                    item_memorytitle.setText(resGroup.getGname());
//                                    TextView item_members = (TextView) headview.findViewById(R.id.item_members);
//                                    item_members.setText("...");
//                                    TextView item_time = (TextView) headview.findViewById(R.id.item_time);
//                                    if (!StringUtils.isEmpty(resGroup.getGeventnum())) {
//                                        item_time.setText(resGroup.getGeventnum());
//                                    } else {
//                                        item_time.setText("0");
//                                    }
////                                  headview.attachTo(rv_home_album);
                                } else {
                                    isCantrag22 = false;
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        //showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        BaseApplication.getInstance().mActivitys.remove(this);
        getActivity().unregisterReceiver(mCameraBroadcastReceiver);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }
}